package controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import po.Admin;
import service.AdminService;

@Controller
@RequestMapping("/login")
public class LoginController {
	/**
	 * @author CoinBin
	 */
	
	@Autowired
	private AdminService adminService;
	
	@RequestMapping("/Page")
	public String loginPage(Model model) {
		model.addAttribute("admin", new Admin());
		return "login";
	}
	
	@RequestMapping("/login")
	public String login(@ModelAttribute Admin admin, Model model, HttpSession session) {
		try {
		String adminPwd = adminService.serviceSelect(admin.getAname()).getApwd();
		if(adminPwd.equals(admin.getApwd())) {
			session.setAttribute("loginNow", admin.getAname());
			
			//这个地方最后改成初始界面的位置
			return "toadmin";
		}else {
			model.addAttribute("massage", "用户名或密码错误");
			return "forward:/login/Page";
		
		}
		
		}catch (Exception e) {
			// TODO: handle exception\
			model.addAttribute("massage", "查询不到该用户名");
			return "forward:/login/Page";
		}
	}
	
	@RequestMapping("/logout")
	public String logOut(Model model, HttpSession session) {
		session.removeAttribute("loginNow");
		return "forward:/login/Page";
	}
	
	@RequestMapping("/admin")
	public String admin() {
		return "admin";
	}
}
